package dj.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.ModelDriven;

import dj.action.base.BaseAction;
import dj.model.ZhebiaoXishu;
import dj.pageModel.Json;
import dj.pageModel.PzhebiaoXishu;
import dj.service.BaseService;
import dj.service.ZhebiaoxishuService;

@Action("zbxsAction")
public class ZhebiaoxishuAction extends BaseAction implements ModelDriven<PzhebiaoXishu> {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(ZhebiaoxishuAction.class);

	private PzhebiaoXishu px = new PzhebiaoXishu();

	private BaseService<ZhebiaoXishu> baseService;

	private ZhebiaoxishuService zbxsService;

	public PzhebiaoXishu getModel() {
		return px;
	}

	public BaseService<ZhebiaoXishu> getBaseService() {
		return baseService;
	}

	@Autowired
	public void setBaseService(BaseService<ZhebiaoXishu> baseService) {
		this.baseService = baseService;
	}

	public ZhebiaoxishuService getZbxsService() {
		return zbxsService;
	}

	@Autowired
	public void setZbxsService(ZhebiaoxishuService zbxsService) {
		this.zbxsService = zbxsService;
	}

	public void getZhebiaoxishuList() {
		Map<String, Object> zbxsMap = new HashMap<String, Object>();
		String p = ServletActionContext.getRequest().getParameter("page");
		String r = ServletActionContext.getRequest().getParameter("rows");
		int page = 0;
		int rows = 0;
		try {
			page = Integer.parseInt(p);
			rows = Integer.parseInt(r);
		} catch (NumberFormatException e1) {
			e1.printStackTrace();
		}
		try {
			// Map<String, Object> params = new HashMap<String, Object>();
			// Map<String, String> conditions = new HashMap<String, String>();
			String order = "order by createDate desc, status";
			long zbxsNum = baseService.getObjectNum(ZhebiaoXishu.class, null, null);
			List<ZhebiaoXishu> zbxss = baseService.getObjcetPagination(ZhebiaoXishu.class, null, null, page, rows, order);

			List<PzhebiaoXishu> zbxs_list = new ArrayList<PzhebiaoXishu>();
			if (null != zbxss && zbxss.size() > 0) {
				for (ZhebiaoXishu zbxs : zbxss) {
					PzhebiaoXishu pzhebiaoXishu = new PzhebiaoXishu();
					BeanUtils.copyProperties(zbxs, pzhebiaoXishu);
					zbxs_list.add(pzhebiaoXishu);
				}
			}
			zbxsMap.put("total", zbxsNum);
			zbxsMap.put("rows", zbxs_list);
		} catch (Exception e) {
			e.printStackTrace();
		}

		super.writeToJson(zbxsMap);
	}

	/**
	 * 新增折标系数
	 */
	public void addZhebiaoxishu() {
		Json j = new Json();
		try {
			zbxsService.save(px);
			j.setSuccess(true);
			j.setMsg("保存成功！");
		} catch (Exception e) {
			j.setMsg("保存失败，" + e.getMessage());
			logger.error(e.getMessage());
		}

		super.writeToJson(j);
	}
	
	public void updateZhebiaoxishu() {
		Json j = new Json();
		try {
			zbxsService.update(px);
			j.setSuccess(true);
			j.setMsg("保存成功！");
		} catch (Exception e) {
			j.setMsg("保存失败，" + e.getMessage());
			logger.error(e.getMessage());
		}

		super.writeToJson(j);
	}
	
	public void updateZbxsStatus() {
		Json j = new Json();
		try {
			zbxsService.updateStatus(px);
			j.setSuccess(true);
			j.setMsg("状态更改成功！");
		} catch (Exception e) {
			j.setMsg("状态更改失败，" + e.getMessage());
			logger.error(e.getMessage());
		}

		super.writeToJson(j);
	}
	
	public void getZhebiaoxishuByid() {
		PzhebiaoXishu pbx = new PzhebiaoXishu();
		ZhebiaoXishu zx = zbxsService.getZhebiaoxishuByid(px.getId());
		BeanUtils.copyProperties(zx, pbx);
		
		super.writeToJson(pbx);
	}

}
